from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode

# 数据
categories = ["算法", "图像", "人工智能", "安卓", "Android", "数据", "Java", "前端", "软件", "运维"]
values = [17827, 16994, 16274, 14201, 13910, 13707, 13020, 12365, 11201, 9417]

# 创建柱状图
bar = (
    Bar()
    .add_xaxis(categories)
    .add_yaxis("工资", values,
               itemstyle_opts=opts.ItemStyleOpts(
                   color=JsCode(
                       """
                       function(params) {
                           var colorList = [
                               '#e6f7ff', '#e9d7fe', '#e3f9e5', '#ffc107', '#fde2e4',
                               '#fff5b1', '#e6fff2', '#f4e2d8', '#ffe7ba', '#00bcd4'
                           ];
                           return colorList[params.dataIndex % 10];
                       }
                       """
                   )
               )
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="工资表", pos_left="center"),
        legend_opts=opts.LegendOpts(pos_right="right"),  # 图例靠右对齐
        xaxis_opts=opts.AxisOpts(axislabel_opts={"rotate": 15}),  # 旋转X轴标签，避免重叠
        yaxis_opts=opts.AxisOpts(name="工资"),
        tooltip_opts=opts.TooltipOpts(is_show=True),  # 显示提示框
    )
    .set_series_opts(
        label_opts=opts.LabelOpts(is_show=True, position="inside")  # 显示数据标签在柱子内部
    )
)

# 渲染图表到 HTML 文件
bar.render("6.2(2) 全国.html")